<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


class NewController extends AdminController{
    
    
     public function filters()
    {
        return array(
            'ajaxOnly + hot'
        );
    }
    
    
    
   
    
    public function actionIndex(){
        
        $News = new News();
        $News->unsetAttributes();
        
        
        
        if(isset($_GET['News'])){
            $News->attributes=$_GET['News'];
        } 
        
       //设置热点
        if(Yii::app()->request->isAjaxRequest){
           if(isset($_POST['id'])&&isset($_POST['is_hot'])){
                if($_POST['is_hot'] ==2){ 
                     $sql_a = "update xyh_news set is_hot=2 where is_hot=1";
                    $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
                     $sql_a = "update xyh_news set is_hot=1 where id={$_POST['id']}";
                   $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
                } 
           }
       }
        
        $this->render('index',array(
               'News'=>$News  
        ));
        
    }
    
    public function actionAdd(){
        
       $News = new News();
       $News->unsetAttributes();
       $News->is_hot =2;
       
       
        if((isset($_POST['ajax']) && $_POST['ajax']==='news-article'))
        {
              //两个model ajax验证        
            //p_e($_POST);           
            echo CActiveForm::validate(array($News));  
            Yii::app()->end();
  
        }
       
        if(isset($_POST['News']) && $_POST['News']){

            //p($_POST);
           if($_POST['News']['is_hot'] ==1){ 
                $sql_a = "update xyh_news set is_hot=2 where is_hot=1";
                $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
                  
            }
            
            $News ->attributes = $_POST['News'];
            //$News->alumni_id = $_SESSION['alumni_id']; 
            $News->ctime = time();
            $News->status =1;
            $News->is_del=2;
            
            
           // p_e($News->attributes);
            if($News->save()){ 
                 Yii::app()->user->setFlash('addform',"新闻添加成功!");
                Yii::app()->jump->success("新闻添加成功");
               
            }else{
                
                Yii::app()->user->setFlash('addform',"新闻添加失败!");
                $ErrorMessage = BootExtend::GetErrorMassage($News);
                Yii::app()->jump->error('添加失败!'.'错误信息--('.$ErrorMessage.')');

                barek; 
            }
            
        }
        $this->render('add',array('News'=>$News));
  
    }
    
    
    
    public function actionView($id){
        
        $sql ="SELECT n.id,n.title,n.content,n.banner_url,n.ctime,n.zan_count,n.fav_count,count(n_c.id) as  commentNum 
               FROM xyh_news as n
               LEFT JOIN xyh_news_comments as n_c on n.id = n_c.news_id
               WHERE n.id = {$id} ";
                
                
        $News = Yii::app()->db->createCommand($sql)->queryAll();
        
      // p_e($News);
        
        if($News){
             $this->renderPartial('view',array('News'=>$News[0]),FALSE,TRUE);
        }else{
             throw new CHttpException(404,"无此新闻！");
             
        }
        
       
    }
    
    public function actionEdit($id){
       
        $News = News::model()->findByPk($id);        
       
        
         if((isset($_POST['ajax']) && $_POST['ajax']==='news-form'))
        {
              //两个model ajax验证        
            //p_e($_POST);           
            echo CActiveForm::validate(array($News));  
            Yii::app()->end();
  
        }
       
        if(isset($_POST['News']) && $_POST['News']){
            
            if($_POST['News']['is_hot'] ==1){ 
                   $sql_a = "update xyh_news set is_hot=2 where is_hot=1";
                  $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
    
            } 
            
            $News->mtime =time();
            $News ->attributes = $_POST['News'];

            
           // p_e($News ->attributes);
            if($News->save()){
                    
                    Yii::app()->jump->success("新闻添加成功");
            
            }else{
                
              
                 Yii::app()->user->setFlash('addform',"新闻添加失败!");
                $ErrorMessage = BootExtend::GetErrorMassage($News);
                Yii::app()->jump->error('添加失败!'.'错误信息--('.$ErrorMessage.')');
                
                barek; 
            }
            
        }
        
        
       
        if($News){
             $this->render('edit',array('News'=>$News));
        }else{
             throw new CHttpException(404,"无此新闻！");
             
        }
        
        
    }
    
    
    
    public function actionDelete($id){
        
        if(Yii::app()->request->isAjaxRequest){
            if($id){
                $News = News::model()->findByPk($id);
                $News->is_del=1;
                $News->status=2;
                
               // $StoreData->name=$StoreData->name.'-'.date('Y-m-d H:i:s',time());
                if($News->save(FALSE))
                {
                    echo '删除成功';
                }else{
                   var_dump($News->getErrors());
                }
            }
            
        }
        
    }
    
    /*********
     * 设置热点
     * 接受参数 id
     * 
     */
//    public function actionHot()
//    { 
//       if(Yii::app()->request->isAjaxRequest){
//           if(isset($_POST['id'])&&isset($_POST['status'])){
//                
//                if($_POST['status'] ==1){
//                    
//                    $sql_a = "update xyh_news set status=2 where id=".$_POST['id'];
//                    $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();                     
//                }
//                if($_POST['status'] ==2){
//                    
//                    $sql_a = "update xyh_news set status=1 where id=".$_POST['id'];
//                    $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();                
//                } 
//           }
//       }
//    }
    
    public  function actionUploads()
    {
        
           $imgUrl ='http://ti.211xyh.com';
         $imgUrlPath=Yii::app()->basePath.'/../';
         
           if(Yii::app()->request->isAjaxRequest)
        {
            $is_thum =0; //缩略图判断值   
             if($_POST['img_type']=='file_upload_ws'){
                   $targ_w =750; $targ_h =445; 
             }
             
             if($_POST['img_type']=='file_upload_ws_min'){
                   $targ_w =100; $targ_h =100; 
                   $is_thum=1;
             }
          
          // $url_root = explode($_SERVER['HTTP_HOST'],$_POST['input_img_url']);
         //  $src =  $_SERVER['DOCUMENT_ROOT'] . $url_root[1];
		   
		   
		    $url_root = explode($imgUrl,$_POST['input_img_url']); //服务器特用
          $src =  $imgUrlPath . $url_root[1];
           
           $pathinfos = pathinfo($url_root[1]);
           $src_path = $pathinfos['extension'];
         
           //文件判断
          switch ($src_path)
          {
            case 'jpg':
                    $img_r = imagecreatefromjpeg($src);
                break;
            case 'jpeg':
                    $img_r = imagecreatefromjpeg($src);
                break;
            case 'png':
                    $img_r = imagecreatefrompng($src);
                break;

            default:
                    echo 4;
               exit;
                break;
           }

           $dst_r = ImageCreateTrueColor($targ_w,$targ_h);
           $pwd_ture=imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
           $targ_w,$targ_h,$_POST['w'],$_POST['h']);
           @imagejpeg($dst_r,$src);
           
           //判断是否缩略图
           if($is_thum){
                //判断是否修改页
                if(isset($_POST['input_img_id']) && !empty($_POST['input_img_id']) &&$pwd_ture){
                    $sql_a = "update xyh_news set min_banner_url='{$_POST['input_img_url']}' where id=".$_POST['input_img_id'];
                    $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
                }
               
             
               
           }else{
               //判断是否修改页
                if(isset($_POST['input_img_id']) && !empty($_POST['input_img_id']) &&$pwd_ture){
                    $sql_a = "update xyh_news set banner_url='{$_POST['input_img_url']}' where id=".$_POST['input_img_id'];
                    $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
                }
           }
            
           //返回信息
            if($pwd_ture)
                echo $_POST['input_img_url'];
            else
                echo 3;
           
        
        }
    }
    
    
    
   
}